//
//  BSLAsset.m
//  demo
//
//  Created by RYAN on 15/7/15.
//
//

#import "BSLAsset.h"
#import "BSLGalleryHelper.h"

@implementation BSLAsset

- (id)initWithAsset:(ALAsset *)asset{
    self = [super init];
    
    if (self) {
        self.alAsset = asset;
        NSString *type = [self.alAsset valueForProperty:ALAssetPropertyType];
        self.isVideo = [type isEqualToString:ALAssetTypeVideo];

        if (self.isVideo) {
            ALAssetRepresentation *rep = [asset defaultRepresentation];
            NSString *uti = [rep UTI];
            NSDictionary *dic = [asset valueForProperty:ALAssetPropertyURLs];
            NSURL *url= [dic objectForKey:uti];
            AVAsset *video = [AVAsset assetWithURL:url];
            self.avAsset = video;
            self.videoURL = url;
        }
    }
    
    return self;
}

- (NSString *)timeFormatted:(int)totalSeconds{
    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;
    
    NSString *timeFormat = @"00:00";
    if (totalSeconds/3600 >0) {
        //大于1小时
       timeFormat = [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds];

    }
    
    else if (totalSeconds/60 > 0){
        //大于1分钟
        timeFormat = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];
    }
    
    else{
        timeFormat = [NSString stringWithFormat:@"00:%02d", seconds];
    }
    
    return timeFormat;
}

- (NSString *)videoDuration{
    if (!_videoDuration) {
        
        Float64 second = CMTimeGetSeconds([self.avAsset duration]);
        _videoDuration = [self timeFormatted:(int)second];
    }
    
    return _videoDuration;
}

- (UIImage *)fullResolutionImage{
    if (self.isVideo) {
        //TODO when is video
        return nil;
    }
    
    else if (!_fullResolutionImage){
        
        ALAsset *asset = self.alAsset;
        ALAssetRepresentation *rep = [asset defaultRepresentation];
        NSString *adj = [rep metadata][@"AdjustmentXMP"];
        CGImageRef fullResImage = [rep fullResolutionImage];;
        
        if (adj){
            //若被修改过，不为nil
            fullResImage = [rep fullResolutionImage];
            NSData *xmlData = [adj dataUsingEncoding:NSUTF8StringEncoding];
            CIImage *image = [CIImage imageWithCGImage:fullResImage];
            NSError *error = nil;
            NSArray *filters = [CIFilter filterArrayFromSerializedXMP:xmlData inputImageExtent:[image extent] error:&error];
            CIContext *context = [CIContext contextWithOptions:nil];
            
            if (filters && !error) {
                for (CIFilter *filter in filters) {
                    [filter setValue:image forKey:kCIInputImageKey];
                    image = [filter outputImage];
                }
                fullResImage = [context createCGImage:image fromRect:[image extent]];
            }
            
        }
        
        UIImage *assetImage = [UIImage imageWithCGImage:fullResImage scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
        _fullResolutionImage = assetImage;
    }
    
    return _fullResolutionImage;
}

- (NSData *)dataWithFullResolutionImage{
    NSData *result = nil;
    if (self.isVideo) {
        //TODO when is video
    }
    
    else{
        
        UIImage *assetImage = [self fullResolutionImage];
        NSData *data = [[BSLGalleryHelper shareInatance] isPNGPathExtension] ? UIImagePNGRepresentation(assetImage) : UIImageJPEGRepresentation(assetImage, 1.0);
        result = data;
    }
    
    return result;
}

- (UIImage *)aspectRatioThumbnail{
    if (!_aspectRatioThumbnail && !self.isVideo) {
        
        CGImageRef ref = [self.alAsset aspectRatioThumbnail];
        _aspectRatioThumbnail = [UIImage imageWithCGImage:ref];
    }
    
    return _aspectRatioThumbnail;
}

- (UIImage *)fullScreenImage{
    ALAsset *asset = self.alAsset;
    ALAssetRepresentation *rep = [asset defaultRepresentation];
    CGImageRef ref = [rep fullScreenImage];
    UIImage *img = [UIImage imageWithCGImage:ref];
    return img;
}

- (UIImage *)thumbnail{
    ALAsset *asset = self.alAsset;
    CGImageRef ref = [asset thumbnail];
    UIImage *img = [UIImage imageWithCGImage:ref];
    return img;
}

- (NSString *)filename{
    ALAsset *asset = self.alAsset;
    ALAssetRepresentation *rep = [asset defaultRepresentation];
    return [rep filename];
}

@end
